package com.yicong.taxi.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yicong.taxi.api.ApiClient;
import com.yicong.taxi.api.observers.EntryResultObserver;
import com.yicong.taxi.base.BaseActivityPresenter;
import com.yicong.taxi.config.MyApp;
import com.yicong.taxi.iview.RegisterView;
import com.yicong.taxi.model.MsgAuth;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class RegisterPresenter extends BaseActivityPresenter<RegisterView> {
    public RegisterPresenter(Activity activity, RegisterView iView, LifecycleProvider<ActivityEvent> provider) {
        super(activity, iView, provider);
    }

    public void register(String name,String tel,String idFront,
                         String idBack,String drivingFront,String drivingBack,
                         String obtain,String license){
        Map<String, Object> map = new LinkedHashMap<>();//body  map
        map.put("db", MyApp.db);
        map.put("function","app_insert_login");
        map.put("login_id","0");
        map.put("name",name);
        map.put("tel",tel);
        map.put("pic_shenfenzheng_1",idFront);
        map.put("pic_shenfenzheng_2",idBack);
        map.put("pic_jiashizheng_1",drivingFront);
        map.put("pic_jiashizheng_2",drivingBack);
        map.put("pic_congyezige",obtain);
        map.put("pic_shanggangzheng",license);
        map.put("remark","备注：无");
        String json= new Gson().toJson(map);//要传递的json
        RequestBody requestBody= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ApiClient.getApiRetrofitInstance()
                .register(requestBody)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(provider.bindToLifecycle())
                .subscribe(new EntryResultObserver<MsgAuth>(activity) {
                    @Override
                    public void onSuccess(MsgAuth msgAuth) {
                        if(iView!=null){
                            iView.registerSuccess();
                        }
                    }
                });
    }
}
